options compress=YES;
libname manasi ''; 

* THIS PROGRAM CREATES ONE RECORD FOR EACH SSI CHILD WITH ALL RECORD
ESTABLISHED DATES TO MERGE THE CORRECT SSR RECORD INTO OTHER FILES;

* Create ID variable for records;
data manasi.kids_ssr_full_estpdonly_wide;
	set manasi.kids_ssr_full_estpdonly;
	recno + 1;
	by hun;
	if first.hun then recno=1;
run;

* Find out how many records per kid ;
proc means data=manasi.kids_ssr_full_estpdonly_wide noprint;
	by hun;
	var recno;
	output out=manasi.kids_ssr_full_estpdonly_stats max(recno)=max_recno;
run;

proc means mean median min max p75 p90 p95 p99 data=manasi.kids_ssr_full_estpdonly_stats;
	var max_recno;
run;

data manasi.kids_ssr_full_estpdonly_wide;
	merge 	
			manasi.kids_ssr_full_estpdonly_wide 
			manasi.kids_ssr_full_estpdonly_stats
				(keep = hun max_recno);
	by hun; 
run;

data manasi.kids_ssr_full_estpdonly_wide;
	set manasi.kids_ssr_full_estpdonly_wide;
	if recno<=7;
run;

* Reshape wide;
data manasi.kids_ssr_full_estpdonly_wide;
	set manasi.kids_ssr_full_estpdonly_wide;
	by hun;

	keep 	hun 
			estpd1-estpd7
			;	

	retain 	estpd1-estpd7
			;

	array 	aestpd(1:7) 		estpd1-estpd7 ; 

	if first.hun then
	do;
		do i=1 to 7;
			aestpd(i)=.;
		end;
	end;

	aestpd(recno) = n_estpd_num ;	

	if last.hun then output;

run;
